/*
 * @Authoryour name
 * @Date2020-12-17 23:14:49
 * @LastEditTime2020-12-17 23:14:49
 * @LastEditorsyour name
 * @DescriptionIn User Settings Edit
 * @FilePath\STM32H750_FreeRTOS\Bsp\bsp_sdram\bsp_sdram.h
 */ 
#ifndef BSP_FMC_SDRAM_H_
#define BSP_FMC_SDRAM_H_

#include "common_types.h"
#include "stm32h7xx_hal.h"

#define FMC_SDRAM_GPIO_CLK_EN()          \
    do{                                  \
        __HAL_RCC_GPIOC_CLK_ENABLE();    \
        __HAL_RCC_GPIOD_CLK_ENABLE();    \
        __HAL_RCC_GPIOE_CLK_ENABLE();    \
        __HAL_RCC_GPIOF_CLK_ENABLE();    \
        __HAL_RCC_GPIOG_CLK_ENABLE();    \
        __HAL_RCC_GPIOH_CLK_ENABLE();    \
    }while(0)

#define FMC_SDRAM_A0_9_PORT            GPIOF
#define FMC_SDRAM_A10_12_PORT          GPIOG
#define FMC_SDRAM_A0_PIN               GPIO_PIN_0
#define FMC_SDRAM_A1_PIN               GPIO_PIN_1
#define FMC_SDRAM_A2_PIN               GPIO_PIN_2
#define FMC_SDRAM_A3_PIN               GPIO_PIN_3
#define FMC_SDRAM_A4_PIN               GPIO_PIN_4
#define FMC_SDRAM_A5_PIN               GPIO_PIN_5
#define FMC_SDRAM_A6_PIN               GPIO_PIN_12
#define FMC_SDRAM_A7_PIN               GPIO_PIN_13
#define FMC_SDRAM_A8_PIN               GPIO_PIN_14
#define FMC_SDRAM_A9_PIN               GPIO_PIN_15
#define FMC_SDRAM_A10_PIN              GPIO_PIN_0
#define FMC_SDRAM_A11_PIN              GPIO_PIN_1
#define FMC_SDRAM_A12_PIN              GPIO_PIN_2

#define FMC_SDRAM_D0_3_13_15_PORT      GPIOD
#define FMC_SDRAM_D4_12_PORT           GPIOE
#define FMC_SDRAM_D0_PIN               GPIO_PIN_14
#define FMC_SDRAM_D1_PIN               GPIO_PIN_15
#define FMC_SDRAM_D2_PIN               GPIO_PIN_0
#define FMC_SDRAM_D3_PIN               GPIO_PIN_1
#define FMC_SDRAM_D4_PIN               GPIO_PIN_7
#define FMC_SDRAM_D5_PIN               GPIO_PIN_8
#define FMC_SDRAM_D6_PIN               GPIO_PIN_9
#define FMC_SDRAM_D7_PIN               GPIO_PIN_10
#define FMC_SDRAM_D8_PIN               GPIO_PIN_11
#define FMC_SDRAM_D9_PIN               GPIO_PIN_12
#define FMC_SDRAM_D10_PIN              GPIO_PIN_13
#define FMC_SDRAM_D11_PIN              GPIO_PIN_14
#define FMC_SDRAM_D12_PIN              GPIO_PIN_15
#define FMC_SDRAM_D13_PIN              GPIO_PIN_8
#define FMC_SDRAM_D14_PIN              GPIO_PIN_9
#define FMC_SDRAM_D15_PIN              GPIO_PIN_10

#define FMC_SDRAM_BA_PORT              GPIOG
#define FMC_SDRAM_BA0_PIN              GPIO_PIN_4
#define FMC_SDRAM_BA1_PIN              GPIO_PIN_5

#define FMC_SDRAM_NBL_PORT             GPIOE
#define FMC_SDRAM_NBL0_PIN             GPIO_PIN_0
#define FMC_SDRAM_NBL1_PIN             GPIO_PIN_1

#define FMC_SDRAM_SDNE0_PORT           GPIOC
#define FMC_SDRAM_SDNE0_PIN            GPIO_PIN_2

#define FMC_SDRAM_SDCKE0_PORT          GPIOC
#define FMC_SDRAM_SDCKE0_PIN           GPIO_PIN_3

#define FMC_SDRAM_SDNCAS_PORT          GPIOG
#define FMC_SDRAM_SDNCAS_PIN           GPIO_PIN_15

#define FMC_SDRAM_SDNRAS_PORT          GPIOF
#define FMC_SDRAM_SDNRAS_PIN           GPIO_PIN_11

#define FMC_SDRAM_SDNWE_PORT           GPIOH
#define FMC_SDRAM_SDNWE_PIN            GPIO_PIN_5

#define FMC_SDRAM_SDCLK_PORT           GPIOG
#define FMC_SDRAM_SDCLK_PIN            GPIO_PIN_8

#define FMC_SDRAM_CLK_EN()             __HAL_RCC_FMC_CLK_ENABLE();

#define SDRAM_MODEREG_BURST_LENGTH_1              0x0000
#define SDRAM_MODEREG_BURST_LENGTH_2              0x0001
#define SDRAM_MODEREG_BURST_LENGTH_4              0x0002
#define SDRAM_MODEREG_BURST_LENGTH_8              0x0003
#define SDRAM_MODEREG_BURST_FULL_PAGE             0x0007
#define SDRAM_MODEREG_ADDR_MODE_SEQUENTIAL        0x0008
#define SDRAM_MODEREG_ADDR_MODE_INTERLEAVE        0x0000
#define SDRAM_MODEREG_CAS_LATENCY_2               0x0020
#define SDRAM_MODEREG_CAS_LATENCY_3               0x0030
#define SDRAM_MODEREG_BURST_READ_WRITE            0x0000
#define SDRAM_MODEREG_BURST_READ_SINGLE_WRITE     0x0200

#define SDRAM_BANK_ADDR                 ((uint32_t)0XC0000000)
#define SDRAM_SIZE                      ((uint32_t)0x2000000)

extern void bsp_fmc_sdram_init(void);
extern void bsp_fmc_sdram_test(void);

#endif

